﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.auth.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.util.*;



@Service
@Transactional
public class SystemUserServiceImpl extends BaseBusinessService<SystemUser, java.lang.Integer> implements SystemUserService<SystemUser, java.lang.Integer> {

	private SystemUserDao systemUserDao;
	private SystemUserRolesDao systemUserRolesDao;

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写 */
	public void setSystemUserDaoImpl(SystemUserDao dao) {
		this.systemUserDao = dao;
	}

	public void setSystemUserRolesDaoImpl(SystemUserRolesDao dao) {
		this.systemUserRolesDao = dao;
	}

	public EntityDao getEntityDao() {
		return this.systemUserDao;
	}

	@Transactional(readOnly = true)
	public Page findPage(SystemUserQuery query) {
		return systemUserDao.findPage(query);
	}

	public List findUserRole(SystemUserQuery query) {
		// TODO Auto-generated method stub
		return systemUserDao.findUserRole(query);
	}

	public void removeById(Integer id) {
		List l = systemUserDao.getHibernateTemplate().find("from SystemUserRoles t where t.userId=?", id);
		if (l != null && l.size() > 0)
			throw new RuntimeException("用户进行了角色设置,不能删除，前先将用户的角色清空。");

		systemUserDao.deleteById(id);
		systemUserDao.flush();
	}

	public void userRoleSave(Integer userId, List roles) {
		// TODO Auto-generated method stub
		// 查找原来的
		// systemUserRolesDao.
		// List
		// l=systemUserDao.getHibernateTemplate().find("from SystemUserRoles t where t.userId=?",
		// userId);
		// 简单处理，删除所有原来的
		systemUserDao.getHibernateTemplate().bulkUpdate("delete from SystemUserRoles t where t.userId=? ", userId);
		systemUserDao.flush();
		SystemUserRoles systemUserRoles = null;
		for (int i = 0; roles != null && i < roles.size(); i++) {
			systemUserRoles = (SystemUserRoles) roles.get(i);

			systemUserRolesDao.saveOrUpdate(systemUserRoles);
			systemUserRolesDao.flush();
		}
		// 新增
		// 多余的删除

	}

	public void save(SystemUser systemUser) {
		//
		systemUserDao.save(systemUser);
		// 保存title
		String titleIdString = systemUser.getTitleIdString();
		String[] titleIds = null;
		if (!StringHelper.isEmpty(titleIdString))
			titleIds = titleIdString.split(",");
		for (int i = 0; titleIds != null && titleIds.length > 0 && i < titleIds.length; i++) {
			SystemUserTitle systemUserTitle = new SystemUserTitle();
			systemUserTitle.setUserId(systemUser.getId());
			systemUserTitle.setTitleId(StringHelper.parseInteger(titleIds[0], 0));
			systemUserTitle.setCreator(systemUser.getCreator());
			systemUserTitle.setCreateDt(new Date());
			systemUserDao.getHibernateTemplate().save(systemUserTitle);
		}
		systemUserDao.flush();

	}

	public void update(SystemUser systemUser) {
		systemUserDao.update(systemUser);
		systemUserDao.flush();
		//--------
		if (true) return;
		// 保存或更新titleid
		String titleIdString = systemUser.getTitleIdString();
		String[] titleIds = null;
		if (!StringHelper.isEmpty(titleIdString))
			titleIds = titleIdString.split(",");
		// 查询已有的
		List l = systemUserDao.getHibernateTemplate().find("from SystemUserTitle t where t.userId=?", systemUser.getId());

		// 删除
		SystemUserTitle systemUserTitle = null;
		SystemUserTitle newSystemUserTitle = null;
		for (int j = 0; l != null && j < l.size(); j++) {
			systemUserTitle = (SystemUserTitle) l.get(j);
			boolean isDelete = true;
			for (int i = 0; titleIds != null && titleIds.length > 0 && i < titleIds.length; i++) {
				if (systemUserTitle.getTitleId().equals(StringHelper.parseInteger(titleIds[i], 0)))
					isDelete = false;
			}
			if (isDelete) {
				System.out.println("开始删除***：" + systemUserTitle.getId());
				systemUserDao.getHibernateTemplate().delete(systemUserTitle);
				systemUserDao.flush();
			}
		}
		// 新增
		System.out.println("update：" + titleIdString);
		for (int i = 0; titleIds != null && titleIds.length > 0 && i < titleIds.length; i++) {
			boolean isAdd = true;
			System.out.println("开始新增：" + titleIds[i]);
			for (int j = 0; l != null && j < l.size(); j++) {
				systemUserTitle = (SystemUserTitle) l.get(j);
				if (systemUserTitle.getTitleId().equals(StringHelper.parseInteger(titleIds[i], 0)))
					isAdd = false;
			}
			newSystemUserTitle = new SystemUserTitle();
			newSystemUserTitle.setUserId(systemUser.getId());
			newSystemUserTitle.setTitleId(StringHelper.parseInteger(titleIds[i], 0));
			newSystemUserTitle.setCreator(systemUser.getModifier());
			newSystemUserTitle.setCreateDt(new Date());
			if (isAdd) {
				// systemUserDao.getHibernateTemplate().save(SystemUserTitle.class.getName(),
				// systemUserTitle);
				systemUserDao.getHibernateTemplate().save(newSystemUserTitle);
				System.out.println("开始新增ac***：" + titleIds[i]);
				systemUserDao.flush();
			}
		}
		systemUserDao.flush();

	}

	public void updateUser(SystemUser systemUser) {
		systemUserDao.update(systemUser);
		systemUserDao.flush();

	}

	public SystemUser findByUsername(String username) {
		// TODO Auto-generated method stub
		List l = systemUserDao.getHibernateTemplate().find("from SystemUser t where t.account=?", username);
		if (l != null && l.size() > 0)
			return (SystemUser) l.get(0);
		return null;
	}

	@Override
	public boolean validateIP(String ip) {
		return systemUserDao.validateIP(ip);
	}

}
